public class Conversation {
	public String ConversationGroupName;
	private Message Head;
	private Message Tail; // Configuring Tail for adding message to the last of
							// the conversation
	public Conversation Next;

	// Adding message
	public void addMessage(String message, String userName) {
		Message msg = new Message();
		msg.SenderName = userName;
		msg.Text = message;

		if (Head != null) {
			Tail.Next = msg;
			Tail = Tail.Next;

		} else {
			Head = msg;
			Tail = Head;
		}

	}

	// Cloning message for getting messages for the search
	private Message clone(Message msg) {
		Message msgHolder = new Message();
		msgHolder.SenderName = msg.SenderName;
		msgHolder.Text = msg.Text;
		return msgHolder;
	}

	// Searching text in the messages
	public Message searchForTextInMessages(String Text) {
		Message msgHolder = null;
		Message searchMessage = Head;
		while (searchMessage != null) {
			if (searchMessage.Text.contains(Text)) {
				if (msgHolder == null) {
					msgHolder = clone(searchMessage);
				} else {
					Message newMsg = clone(searchMessage);
					newMsg.Next = msgHolder;
					msgHolder = newMsg;
				}
			}
			searchMessage = searchMessage.Next;
		}
		return msgHolder;
	}

	public String toString() {
		String retVal = ConversationGroupName + "\n";
		Message temp = Head;
		while (temp != null) {
			retVal += temp.toString() + "\n";
			temp = temp.Next;
		}

		return retVal;
	}
}
